package com.gok.pay.rest;

import com.gok.pay.base.RestResult;
import com.gok.persistence.service.BaseDao;
import com.gok.persistence.service.IBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.UUID;

@Slf4j
public class AbstractBaseRest {
    public static final String ERROR_UNSUPPORT = "非法操作,处理失败(%s),如有疑问请反馈客服!";
    @Autowired
    @Qualifier("baseDao")
    protected BaseDao baseDao;

    @Autowired
    @Qualifier("baseService")
    protected IBaseService baseService;

    public static final String APPLICATION_JSON_UTF8_VALUE = "application/json;charset=UTF-8";

    protected ResponseEntity success() {
        return new ResponseEntity(RestResult.SUCCESS, HttpStatus.OK);
    }

    //单个值类型
    protected ResponseEntity successValue(Object object) {
        return new ResponseEntity(RestResult.successValue(object), HttpStatus.OK);
    }

    protected ResponseEntity successString(String object) {
        return new ResponseEntity(RestResult.successString(object), HttpStatus.OK);
    }
    //对象类型
    protected ResponseEntity successObject(Object object) {
        return new ResponseEntity(RestResult.successObject(object), HttpStatus.OK);
    }

    protected ResponseEntity failure(String code, String msg) {
        log.error("failure msg :{}", msg);
        return new ResponseEntity(new RestResult(code, msg), HttpStatus.BAD_REQUEST);
    }
    protected ResponseEntity failureIfException(Exception e, String msg) {
        return failureIfException(HttpStatus.BAD_REQUEST, e, msg);
    }
    protected ResponseEntity failureIfException(HttpStatus status, Exception e, String msg) {
        log.error("failure", e);
        log.error("failure msg :{}", msg);
        return new ResponseEntity(new RestResult(RestResult.CODE_DEFAULT_FAILURE, msg + (e != null ? e.getMessage() : "")), status);
    }
    protected ResponseEntity failureDefault(String msg) {
        log.error("failure msg :{}", msg);
        return new ResponseEntity(RestResult.failureDefault(msg), HttpStatus.BAD_REQUEST);
    }
    protected ResponseEntity failureUnsupport(String msg) {
        String uuid = UUID.randomUUID().toString();
        log.error("failure msg :{} {}", msg, uuid);
        return new ResponseEntity(RestResult.failureDefault(String.format(ERROR_UNSUPPORT, uuid)), HttpStatus.BAD_REQUEST);
    }
}
